Git and Github

Murray Logan

28 July 2024

Background

Required

  • RStudio or text editor

Version control

Version control

  • large space requirements
  • difficult to collaborate

Git

Git - overview

  • Git - is a distributed version control system
  • Git - stores snapshots of the filesystem
  • commits

  • The system consists of three trees:

Git - overview

Files can be in one of four states:

  • untracked

Git - overview

Files can be in one of four states:

  • untracked
  • staged

Git - overview

Files can be in one of four states:

  • untracked
  • staged
  • committed

Git - overview

Files can be in one of four states:

  • untracked
  • staged
  • committed
  • modified

Git - overview

Git - overview

Git in depth

Git - configuration

This is a once off action (per machine)

  • define who you are
git config --global user.name "Your name"
git config --global user.email "your_email@whatever.com"
  • Specify that the initial branch should be called main rather than master
git config --global init.defaultBranch main
  • Check these settings
git config --global --list
user.name=pcinereus
user.email=i.obesulus@gmail.com
init.defaultbranch=main

Git - configuration

Or within R (via the usethis package)

usethis::use_git_config(user.name='Your name',
               user.email='your_email@whatever.com',
               scope='user')
usethis::git_sitrep()

OR via the gert package

gert::git_config_global()

Git - new repository

mkdir ~/tmp/Test_repo
cd ~/tmp/Test_repo
git init

RStudio

R

library(usethis)
create_project(path='~/path/project_name', rstudio=TRUE)
use_git()

Git - adding content


Create a file (text, code etc)

  • those using R, call it analysis.R
x=seq(1, 10, len = 1)
y=40 * 2 + rnorm(10, 0, 5)
plot(x, y)



Otherwise, create any kind of file (in the folder we just created)

Git - adding content

Stage the changes (add)

git add <file(s)>

For example:

git add analysis.R

RStudio

gert::git_add('analysis.R')

Git - adding content

Git - .gitignore

RStudio

Examples

  • .RData all files ending in .RData
  • .pdf all files ending in .pdf
  • data/ the entire folder called data

Git - committing

git commit -m 'Initial commit'
[main (root-commit) 57cea5c] Initial commit
 1 file changed, 3 insertions(+)
 create mode 100644 analysis.R
cd ~/tmp/Test_repo
git status
On branch main
nothing to commit, working tree clean

RStudio

gert::git_commit('Initial commit')

Git - committing

Git - additional edits

  • Make some changes to the file, stage (add) and commit
x=seq(1, 10, len=1)
y=40*2 + rnorm(10,0,5)
plot(x,y)
summary(x)
[main 09c5b66] Added summary for x
 1 file changed, 1 insertion(+)

Git - additional edits

  • Lets remove the summary(x) and add it to another file, stage (add) and commit

analysis.R

x=seq(1, 10, len=1)
y=40*2 + rnorm(10,0,5)
plot(x,y)

summary.R

summary(x)
summary(y)
[main c988047] Added summaries for x and y
 2 files changed, 5 insertions(+), 4 deletions(-)
 create mode 100644 summary.R

Git - additional edits

Git - history (logs)

git log --oneline --graph --decorate
* c988047 (HEAD -> main) Added summaries for x and y
* 09c5b66 Added summary for x
* 57cea5c Initial commit

RStudio

Git - history (logs)

gert::git_log(max=10)
                                    commit                           author
1 c9880471bc4b2ca3e4df5afcdf92235061cfed8d pcinereus <i.obesulus@gmail.com>
2 09c5b6675ed33654d52feef754aef3d58e8e132a pcinereus <i.obesulus@gmail.com>
3 57cea5cf96ca1ed64c8c2fea28cd2b57befdd80d pcinereus <i.obesulus@gmail.com>
                 time files merge                       message
1 2024-07-28 10:35:09     2 FALSE Added summaries for x and y\n
2 2024-07-28 10:35:09     1 FALSE         Added summary for x\n
3 2024-07-28 10:35:08     1 FALSE              Initial commit\n

A listing (data.frame) of tracked files

gert::git_ls()
        path filesize            modified             created
1 analysis.R       53 2024-07-28 10:35:09 2024-07-28 10:35:09
2  summary.R       22 2024-07-28 10:35:09 2024-07-28 10:35:09

Git - tags

git tag -a <tag> -m <message>

For example:

git tag -a 'V.1' -m 'Version 1'

R/RStudio

gert::git_tag_create(name='V1', message='Version 1')


Git - tags